package com.object.module.lq.chatroom.service;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.object.module.lq.chatroom.websocketcomfig.ChatHandler;
import com.object.utils.GetUserId;
import com.object.utils.NettyChantTypePojo;
import com.object.utils.NettyResponseData;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

import java.util.Map;

public interface NettyMsgService
{
    void execute(ChannelHandlerContext context, NettyChantTypePojo nettyChantTypePojo);

    /**
     * 向通道发送信息
     *
     * @param nettyChantTypePojo
     * @param channel
     */
    default void sendTo(NettyChantTypePojo nettyChantTypePojo, Channel channel)
    {
        NettyResponseData.ok().q("data", nettyChantTypePojo).sendOk(channel);
    }

    /**
     * 根据token获取id
     *
     * @param saToken
     * @return
     */
    default Integer byTokenGetId(String saToken)
    {
        return GetUserId.id(saToken);
    }

    /**
     * 根据token获取通道
     *
     * @param saToken
     * @return
     */
    default Channel getChannelByToken(String saToken)
    {
        Integer getId = byTokenGetId(saToken);
        return ChatHandler.getMap().get(getId);
    }
}
